package com.banshuibao.bsb_android.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.banshuibao.bsb_android.Constants;
import com.banshuibao.bsb_android.R;
import com.banshuibao.bsb_android.adapter.AccountantAdapter;
import com.banshuibao.bsb_android.http.HttpResponse;
import com.banshuibao.bsb_android.http.HttpUtil;
import com.banshuibao.bsb_android.model.AccountantModel;
import com.banshuibao.bsb_android.model.AccountantTypeModel;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

/**
 * Created by zyh on 2018/5/29.
 */

public class AccountantSearchActivity extends BaseActivity {

    private TextView accountantType;

    private RecyclerView recyclerView;

    private PopupWindow popupWindow;

    private PopWindowAdapter popAdapter;

    private ArrayList<AccountantTypeModel.Data> list;

    private AccountantTypeModel.Data typeData;

    private AccountantAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_accountant_search);

        initView();
    }

    private void initView() {
        setTitleText("会计科目速查");
        accountantType = findViewById(R.id.accountant_type);
        recyclerView = findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new AccountantAdapter(context);
        recyclerView.setAdapter(adapter);
        accountantType.setOnClickListener(l);

        getType();
        getData();
    }

    private void getType() {
        HttpUtil.doPost(Constants.Url.ACCOUNTANT_TYPE_TREE, null, new HttpResponse(context, AccountantTypeModel.class) {
            @Override
            public void onResult(Object obj) {
                AccountantTypeModel accountantTypeModel = (AccountantTypeModel) obj;
                TreeMap<String, String> map = accountantTypeModel.getData();
                if (list == null) {
                    list = new ArrayList<>(map.size() + 1);
                } else
                    list.clear();
                for (TreeMap.Entry<String, String> entry : map.entrySet()) {
                    AccountantTypeModel.Data data = new AccountantTypeModel.Data();
                    data.setId(entry.getKey());
                    data.setName(entry.getValue());
                    list.add(data);
                }
                AccountantTypeModel.Data data1 = new AccountantTypeModel.Data();
                data1.setName("全部科目");
                data1.setId("-1");
                list.add(0, data1);
                typeData = data1;
                if (popAdapter != null && popupWindow != null && popupWindow.isShowing()) {
                    popAdapter.addData(list);
                }
            }
        });
    }

    private void getData() {
        AccountantModel.Body body = new AccountantModel.Body();
        body.setSinceId(0);
        body.setMaxId(1000);
        if (typeData != null && !"-1".equals(typeData.getId()))
            body.setType(typeData.getId());
        HttpUtil.doPost(Constants.Url.ACCOUNTANT_LIST, body, new HttpResponse(context, AccountantModel.class) {
            @Override
            public void onResult(Object obj) {
                AccountantModel model = (AccountantModel) obj;
                if (adapter != null)
                    adapter.addData(model.getData());
            }
        });
    }


    private View.OnClickListener l = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.accountant_type:
                    if (list == null || list.size() == 0)
                        getType();
                    showPopWindow(v);
                    break;
            }
        }
    };

    private void showPopWindow(View v) {
        if (popupWindow == null) {
            View view = View.inflate(context, R.layout.item_accountant_search_menu, null);
            popupWindow = new PopupWindow(view, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, true);
            popupWindow.setBackgroundDrawable(context.getResources().getDrawable(android.R.color.transparent));
            popupWindow.setOutsideTouchable(true);
            popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
                    accountantType.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.icon_search_drop_down, 0);
                }
            });
            RecyclerView recyclerView = view.findViewById(R.id.listView);
            recyclerView.setLayoutManager(new LinearLayoutManager(this));
            View bg = view.findViewById(R.id.popup_bg);
            popAdapter = new PopWindowAdapter();
            recyclerView.setAdapter(popAdapter);
        }
        accountantType.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.icon_search_drop_up, 0);
        if (list != null)
            popAdapter.addData(list);
        else
            return;
        popupWindow.showAsDropDown(v);
    }

    private class PopWindowAdapter extends RecyclerView.Adapter<PopWindowAdapter.ViewHolder> {

        private List<AccountantTypeModel.Data> list;

        PopWindowAdapter() {
            this.list = new ArrayList<>();
        }

        public void addData(List<AccountantTypeModel.Data> list) {
            if (this.list != null) {
                this.list.clear();
                this.list.addAll(list);
                notifyDataSetChanged();
            }
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_accountant_type_menu, parent, false));
        }

        @Override
        public void onBindViewHolder(final ViewHolder holder, int position) {
            final AccountantTypeModel.Data data = list.get(position);
            if (data != null) {
                if (data.getId().equals(typeData.getId())) {
                    holder.textView.setTextColor(getResources().getColor(R.color.colorPrimary));
                } else {
                    holder.textView.setTextColor(getResources().getColor(R.color.text_black));
                }
                holder.textView.setText(data.getName());
                holder.view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (popupWindow != null && popupWindow.isShowing())
                            popupWindow.dismiss();
                        typeData = data;
                        accountantType.setText(typeData.getName());
                        getData();
                    }
                });
            }
        }

        @Override
        public int getItemCount() {
            return list.size();
        }

        public class ViewHolder extends RecyclerView.ViewHolder {

            private TextView textView;

            private View view;

            public ViewHolder(View itemView) {
                super(itemView);
                textView = itemView.findViewById(R.id.name);
                view = itemView;
            }
        }

        public void clear() {
            if (this.list != null) {
                list.clear();
                list = null;
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (adapter != null) {
            adapter.clear();
            adapter = null;
        }
        if (popAdapter != null) {
            popAdapter.clear();
            popAdapter = null;
        }
    }
}
